/**
 * Created by Demon on 2015/6/15.
 */
var BurnPage = function () {
    var inputs = {
        E3: $("#E3"),
        E4: $("#E4"),
        E6: $("#E6"),
        E8: $("#E8"),
        E20: $("#E20"),
        E70: $("#E70"),
        E88: $("#E88"),
        E89: $("#E89"),
        E137: $("#E137"),
        E155: $("#E155"),
        E54: $("#E54"),
        E55: $("#E55"),
        E56: $("#E56"),
        E103: $("#E103"),
        E122: $("#E122"),
        E123: $("#E123"),
        E170: $("#E170"),
        E171: $("#E171")
    };

    var popoverMessage = {
        E170: {
            title: "提示信息",
            message: "通常为5 ~ 10℃"
        }
    };

    function popoverHandle() {
        inputs.E170.popover({
            title: popoverMessage.E170.title,
            content: popoverMessage.E170.message,
            placement: "top",
            trigger: "focus"
        })
    }

    function calculateInit() {
        $.ajax({
            url: basePath + "combustion/burn/getCalculateInit",
            dataType: "json",
            type: "post"
        })
    }

    return {
        init: function () {
            popoverHandle();
            calculateInit();
         },

        updata: function (ExParam) {
            //定义保存数据方法
            var save = function (exParam, exVal) {
                if (validate.isNotNumber(exVal)) {
                    validate.invalid();
                    return;
                }
                $.ajax({
                    url: basePath + "combustion/burn/saveValueOfBurnEx",
                    dataType: "json",
                    type: "post",
                    //放了三个值，一个填到默认Ex当中，其他两个分别对应属性值和属性名
                    data: exParam + "=" + exVal + "&paramName=" + exParam + "&paramValue=" + exVal
                });
                validate.valid();
            };
            var $obj = $("#" + ExParam);
            var ExVal = $obj.val();
            validate.init($obj);
            if (ExVal != "") {
                save(ExParam, ExVal);
            } else {
                //判断是否可以计算，如果可以后台计算后返回结果，保存结果，不可以计算提示
                if ($obj.hasClass("calculable")) {
                    $.ajax({
                        url: basePath + "admin/formal/calculate",
                        dataType: "json",
                        type: "post",
                        //燃烧前缀 R
                        data: "paramName=R" + ExParam,
                        success: function (data) {
                            $obj.val(data);
                            save(ExParam, data);
                        }
                    });
                    validate.valid();
                }
                else
                    validate.invalid();
            }
        },
        calcul:function(ExParam) {
            var $obj = $("#" + ExParam);
            var save = function (exParam, exVal) {
                if (validate.isNotNumber(exVal)) {
                    validate.invalid();
                    return;
                }
                $.ajax({
                    url: basePath + "combustion/burn/saveValueOfBurnEx",
                    dataType: "json",
                    type: "post",
                    //放了三个值，一个填到默认Ex当中，其他两个分别对应属性值和属性名
                    data: exParam + "=" + exVal + "&paramName=" + exParam + "&paramValue=" + exVal
                });
                validate.valid();
            };
            $.ajax({
                url: basePath + "admin/formal/calculate",
                dataType: "json",
                type: "post",
                //燃烧前缀 R
                data: "paramName=R" + ExParam,
                success: function (data) {
                    if(data == undefined || data == null || data == 0) {
                        toast.error("信息填写不完整!请仔细核对信息");
                    } else {
                        $obj.val(data);
                        save(ExParam, data);
                    }
                }
            });
        }
    }
}();
